package com.jy.api.controller.admin;

import com.jy.api.dao.EmployeeDao;
import com.jy.api.dao.OrderDao;
import com.jy.api.dao.WorkShiftDao;
import com.jy.api.domain.*;
import com.jy.api.service.WorkshiftService;
import com.jy.api.vo.admin.workshift.WorkShiftDetailsVo;
import com.jy.api.vo.admin.workshift.WorkshiftVo;
import com.jy.api.vo.pos.WorkShift.WorkShiftVo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.HTTP;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by lihu on 2017/8/31.
 */
@RequestMapping(value = "v1/admin/", produces = "application/json;charset=utf-8")
@RestController
@Slf4j
public class AdminWorkshiftController {

    @Autowired
    WorkShiftDao workShiftDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MessageSource msg;

    @Autowired
    WorkshiftService workshiftService;

    @Autowired
    EmployeeDao employeeDao;


    /**
     * 获取当前班次详情
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "workshift/{id}/details")
    public ResponseEntity getWorkshiftDetails(@PathVariable(name = "id") String id) {
        WorkShift workShift = workShiftDao.findOne(id);
        if (workShift == null) {
            return new ResponseEntity("查询失败当前交班信息不存在！", HttpStatus.BAD_REQUEST);
        }
        try {
            return ResponseEntity.ok(workshiftService.getWorkShiftDetails(workShift));
        } catch (Exception ex) {
            log.error("getWorkshiftDetails is error=>id:{}", id, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);

        }
    }

    /**
     * 获取加班信息员工信息
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "workshift/{id}/staff")
    public ResponseEntity getWorkshiftStaff(@PathVariable(name = "id") String id) {
        WorkShift workShift = workShiftDao.findOne(id);
        if (workShift == null) {
            return new ResponseEntity("查询失败当前交班信息不存在！", HttpStatus.BAD_REQUEST);
        }

        if (!workShift.isClosed()) {
            return new ResponseEntity("当前班次未交班！", HttpStatus.BAD_REQUEST);
        }

        return new ResponseEntity(getStaff(workShift.getStoreId(), workShift.getBeginTime(), workShift.getEndTime()), HttpStatus.OK);
    }

    /**
     * 拼接店员返回值
     *
     * @param storeId
     * @param start
     * @return
     */
    private List<WorkShiftVo.Staff> getStaff(String storeId, Date start, Date end) {
        List<WorkShiftVo.Staff> staff = new ArrayList<>();
        Map<String, WorkShiftVo.Staff> map = new HashMap<>();
        List<String> usernames = new ArrayList<>();

        List<Map<String, Object>> list = orderDao.getStoreCommission(storeId, start, end);
        log.info(" test info :{}", list);
        for (Map<String, Object> map1 : list) {
            String creatorId = map1.get("creatorId").toString();
            if (creatorId == null) {
                continue;
            }
            usernames.add(creatorId);
            WorkShiftVo.Staff staff1 = new WorkShiftVo.Staff(Integer.parseInt(map1.get("orderCount").toString()),
                    Double.parseDouble(map1.get("totalAmount").toString()));
            map.put(creatorId, staff1);
        }

        if (usernames.size() > 0) {
            List<Employee> employees = employeeDao.findListByStoreIdAndEnabledAndUsernameIn(storeId,
                    true, usernames);
            for (Employee employee : employees) {
                WorkShiftVo.Staff staff1 = map.get(employee.getUsername());
                if (staff1 != null) {
                    staff1.setCreatorName(employee.getName());
                    staff.add(staff1);
                }
            }
        }

        return staff;
    }


    @RequestMapping(method = RequestMethod.GET, value = "workshift/{id}/products")
    public ResponseEntity getProductsStatistics(@PathVariable(name = "id") String id,
                                                @RequestParam(name = "page", defaultValue = "0") int page,
                                                @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        WorkShift workShift = workShiftDao.findOne(id);
        if (workShift == null) {
            return new ResponseEntity("查询失败当前交班信息不存在！", HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity(workshiftService.getStatistics(workShift, page, pageSize), HttpStatus.OK);
    }

    /**
     * 获取交班记录并返回
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "workshift/list")
    public ResponseEntity getProducts(@RequestParam(name = "storeId") String storeId,
                                      @RequestParam(name = "page", defaultValue = "0") int page,
                                      @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        try {
            //判断传入分页参数信息
            if (page < 0 || pageSize < 1) {
                return new ResponseEntity("分页参数错误", HttpStatus.BAD_REQUEST);
            }
            //设置分页数据
            Pageable pageable = new PageRequest(page, pageSize, Sort.Direction.DESC, "beginTime");
            Page<WorkShift> workShifts = workShiftDao.findListByStoreIdAndClosed(storeId, true, pageable);
            //初始化返回对象
            WorkshiftVo workshiftVo = new WorkshiftVo(workShifts.getTotalElements(), workShifts.getTotalPages());
            for (WorkShift workShift : workShifts) {
                WorkshiftVo.WorkShift workShift1 = new WorkshiftVo.WorkShift();
                BeanUtils.copyProperties(workShift, workShift1);
                workshiftVo.getWorkShifts().add(workShift1);
            }
            return new ResponseEntity(workshiftVo, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getProducts is error=>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping(path = "workshift/{id}")
    public ResponseEntity getWorkshiftDetails(@PathVariable String id, @RequestParam(name = "page", defaultValue = "0") int page,
                                              @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        if (page < 0 || pageSize < 1) {
            return new ResponseEntity("分页参数错误", HttpStatus.BAD_REQUEST);
        }

        WorkShift ws = workShiftDao.findOne(id);

        if (null == ws.getBeginTime()) {
            return new ResponseEntity("该班没有开班时间", HttpStatus.BAD_REQUEST);
        }

        Pageable pageable = new PageRequest(page, pageSize, Sort.Direction.ASC, "createTime");

        Date endTime = ws.getEndTime();
        if (null == endTime) {
            endTime = new Date();
        }

        Page<Order> orders = orderDao.findListByStoreIdAndPaymentTimeBetweenAndStatusIn(ws.getStoreId(), ws.getBeginTime(), endTime, Arrays.asList(OrderStatus.UNFINISHED, OrderStatus.FINISHED), pageable);

        Page<AdminOrderVo> results = orders.map(new Converter<Order, AdminOrderVo>() {
            @Override
            public AdminOrderVo convert(Order source) {
                AdminOrderVo target = new AdminOrderVo();
                BeanUtils.copyProperties(source, target);

                target.source = msg.getMessage("order.source." + source.getSource().name(), null, Locale.CHINA);
                target.type = msg.getMessage("order.type." + source.getType().name(), null, Locale.CHINA);

                return target;
            }
        });

        return new ResponseEntity(results, HttpStatus.OK);
    }


    /**
     * 获取交班打印商品信息
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/workshift/{id}/summary")
    public ResponseEntity getWorkShift(@PathVariable(name = "id") String id) {
        try {
            WorkShift workShift = workShiftDao.findOne(id);
            if (workShift == null) {
                return new ResponseEntity("获取失败！当前班次不存在！", HttpStatus.BAD_REQUEST);
            }
            List<SalesItem> items = workshiftService.queryClerkProdcutStatistcs(workShift.getOperatorId(), workShift.getStoreId(), workShift.getBeginTime(), new Date());
            if (null == items) {
                items = new ArrayList<>();
            }
            List<WorkShiftDetailsVo.Coupon> coupons = workshiftService.getWorkShiftCoupon(workShift.getStoreId(),
                    workShift.getBeginTime(), workShift.getEndTime());
            Map map = new HashMap();
            map.put("types", items);
            map.put("coupons", coupons);
            return new ResponseEntity(map, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("adminIndex  is error==> id{}", id, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Data
    @NoArgsConstructor
    private class AdminOrderVo {
        private String id;
        private String code;
        private Date createTime;
        private Date paymentTime;
        private double amount;
        private String creatorName;
        private int count;
        private String source;
        private String type;
    }

}


